package com.oocl.training.util;

import com.oocl.training.exception.ParameterErrorException;


/**
 * 随机数生成工具 TODO
 * @author GANAB
 *
 */
public class RandomUtil {

	/**
	 * 获取随机数
	 * @param start 区间最小值
	 * @param end 区间最大值
	 * @param modify 希望被修改概率的数字
	 * @param probability 被修改数字概率
	 * @return
	 * @throws ParameterErrorException 
	 */
	public static Integer getRandom(Integer start, Integer end, Integer target, Double probability) throws ParameterErrorException{
		if(start>=end || target<start || target > end || probability < 0|| probability > 1.0){
			throw new ParameterErrorException("Error parameter");
		}
		double range = end-start+1.0;
		double pro = (probability-(1.0/range))/((range-1.0)/range);
		if(pro<0||pro>1){
			throw new ParameterErrorException("Error parameter");
		}
		
		int rs = (int)(Math.random() * (end-start+1))+start;
		double rs2 = (double)(Math.random());
		if(rs2<pro){
			return target;
		}else{
			return rs;
		}
	}

}
